//
//  AllStoreAddress.m
//  AutoHome
//
//  Created by wqh on 15-5-14.
//  Copyright (c) 2015年 Wqh. All rights reserved.
//

#import "AllStoreAddress.h"

@interface AllStoreAddress ()<MKMapViewDelegate,CLLocationManagerDelegate>
@property (nonatomic) CLGeocoder *geocoder;
@property (nonatomic) NSMutableArray *annoArr;
@end

@implementation AllStoreAddress

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.annoArr = [NSMutableArray array];
    self.geocoder = [[CLGeocoder alloc] init];
    
    [self encodeArr];
   
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = YES;
    self.navigationController.navigationBar.translucent = YES;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"displayButton" object:@(0)];
    
}

- (void)displayMapView {
    
    self.mapView.mapType = MKMapTypeStandard;
    self.mapView.zoomEnabled = YES;
    self.mapView.scrollEnabled = YES;
    self.mapView.rotateEnabled = YES;
//    self.mapView.showsUserLocation = YES;
    self.mapView.delegate = self;
    [self.mapView addAnnotations:self.annoArr];
    
    CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    [geocoder geocodeAddressString:self.city completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *placmark = [placemarks firstObject];
        CLLocation *location = placmark.location;
       
        CLLocationCoordinate2D center = {location.coordinate.latitude,location.coordinate.longitude};
        MKCoordinateSpan span;
        span.latitudeDelta = 0.05;
        span.latitudeDelta = 0.05;
        MKCoordinateRegion region = {center,span};
        [self.mapView setRegion:region animated:YES];
    }];

}

- (void)encodeArr {
    if (self.addressArr) {
        for (int i = 0; i<self.addressArr.count; i++) {
            [self encodeAddress:self.addressArr[i] index:i];
        }
        [self displayMapView];
    }
}

//解析字符串地址获得经纬度
- (void)encodeAddress:(NSString *)addres index:(NSInteger)index{
    CLGeocoder *geocoder = [[CLGeocoder alloc]init];
        [geocoder geocodeAddressString:addres completionHandler:^(NSArray *placemarks, NSError *error) {
            //如果解析结果的集合元素的个数大于1 则表明解析得到了经纬度信息
            if (placemarks.count > 0) {
                //只处理第一个解析结果，实际项目中可使用列表让用户选择
                CLPlacemark *placemark = placemarks[0];
                CLLocation *location = placemark.location;
                [self addAnimation:location index:index];
            }
        }];
 
}

//添加锚点
- (void)addAnimation:(CLLocation *)location index:(NSInteger)index{
    MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
    point.coordinate = location.coordinate;
    point.title = self.addressArr[index];
    [self.annoArr addObject:point];
    [self.mapView removeAnnotations:self.mapView.annotations];
    [self.mapView addAnnotations:self.annoArr];
}

#pragma mark MKMapViewDelegate

//自定义锚点控件
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    
    static NSString *annoid = @"AnnoId";
    MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:annoid];
    if (!annoView) {
        annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annoid];
    }
    annoView.image = [UIImage imageNamed:@"pinRed"];
    annoView.canShowCallout = YES;
    return annoView;
}


@end
